﻿/*
 * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
 *
 * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
 *
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
 */

#ifndef UTIL_TIMETICKER_H_
#define UTIL_TIMETICKER_H_

#include <cassert>
#include "util.h"
namespace toolkit {

class Ticker {
public:
    Ticker(uint64_t min_ms = 0) {
        _created = _begin = getCurrentMillisecond();
        _min_ms = min_ms;
    }

    ~Ticker() {
        uint64_t tm = createdTime();
        if (tm > _min_ms) std::cout << "take time: " << tm << "ms" << ", thread may be overloaded" << std::endl;
    }

    uint64_t elapsedTime() const {
        return getCurrentMillisecond() - _begin;
    }

    uint64_t createdTime() const {
        return getCurrentMillisecond() - _created;
    }

    
    void resetTime() {
        _begin = getCurrentMillisecond();
    }

private:
    uint64_t _min_ms = 0;
    uint64_t _begin = 0;
    uint64_t _created = 0;
};

} /* namespace toolkit */
#endif /* UTIL_TIMETICKER_H_ */
